package de.tum.in.eist.poll.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;

import de.tum.in.eist.poll.adapter.ViewCreator;
import de.tum.in.eist.poll.controller.FeedbackController;
import de.tum.in.eist.poll.controller.LectureController;
import de.tum.in.eist.poll.controller.UserController;
import de.tum.in.eist.poll.shared.Student;
import de.tum.in.eist.poll.service.EistPollService;
import de.tum.in.eist.poll.service.EistPollServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EIST_POLL implements EntryPoint {
	
	/**
	 * The view creator handles creation of views
	 */
	ViewCreator view;
	/**
	 * The user controller handles login and logout
	 */
	UserController userController;
	/**
	 * The lecture controller handles lecture access
	 */
	LectureController lectureController;
	/**
	 * handles feedback transmission
	 */
	FeedbackController feedbackController;
	
	/**
	 * Access to the server
	 * @return The Async interface to the server
	 */
	public static EistPollServiceAsync getService() {
		return GWT.create(EistPollService.class);
	}
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// have fun coding! :-)
		view = new ViewCreator();
		userController = new UserController(view);
		lectureController = new LectureController(userController);
		feedbackController = new FeedbackController(lectureController);
		
		History.addValueChangeHandler(new ValueChangeHandler<String>(){

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				// TODO Auto-generated method stub
				chooseMode(event.getValue());
			}
			
		});
		
		
		History.fireCurrentHistoryState();
	}
	
	/**
	 * changes view
	 * @param mode the new view that should be loaded
	 */
	void chooseMode(String mode) {
		if (mode.equalsIgnoreCase("login")) {
			if (userController.isLoggedIn()) {
				History.newItem("user");
			}else
				view.createLogin(userController);
		}else if (mode.equalsIgnoreCase("user")) {
			if (userController.isLoggedIn()) {
				view.createUserView(lectureController);
			}else
				History.newItem("login");
		}else if (mode.matches("lecture_\\d+")) {
			if (userController.isLoggedIn()) {
				view.createLectureView(mode, lectureController, feedbackController);
			}else
				History.newItem("login");
		}else if (mode.equalsIgnoreCase("feedback")) {
			if (userController.isLoggedIn() && lectureController.getSelected() != null && !lectureController.getSelected().containsStudent((Student)userController.getUser())) {
				view.createLectureFeddbackView(feedbackController);
			}
			else if (userController.isLoggedIn())
				History.newItem("user");
			else
				History.newItem("login");
		}
		else {
			if (userController.isLoggedIn())
				History.newItem("user");
			else
				History.newItem("login");
		}
	}
}
